@model IEnumerable<EIMS.Models.Customer>

@{
    ViewData["Title"] = "客户管理";
}

<div class="container-fluid px-4">
    <h1 class="mt-4">@ViewData["Title"]</h1>
    <ol class="breadcrumb mb-4">
        <li class="breadcrumb-item"><a asp-controller="Home" asp-action="Index">首页</a></li>
        <li class="breadcrumb-item active">客户管理</li>
    </ol>
    
    @if (TempData["SuccessMessage"] != null)
    {
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            @TempData["SuccessMessage"]
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    }

    @if (TempData["ErrorMessage"] != null)
    {
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            @TempData["ErrorMessage"]
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    }
    
    <!-- 搜索过滤区域 -->
    <div class="card mb-4">
        <div class="card-header">
            <i class="fas fa-filter me-1"></i>
            筛选条件
        </div>
        <div class="card-body">
            <form method="get" asp-action="Index">
                <div class="row mb-3">
                    <div class="col-md-4">
                        <div class="form-group">
                            <label class="control-label">客户名称/编号/联系人/电话/邮箱</label>
                            <input type="text" name="SearchString" class="form-control" value="@ViewBag.SearchString" />
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group">
                            <label class="control-label">客户类型</label>
                            <select name="Type" class="form-select" asp-items="ViewBag.TypeList">
                                <option value="">所有类型</option>
                            </select>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group">
                            <label class="control-label">状态</label>
                            <select name="Status" class="form-select" asp-items="ViewBag.StatusList">
                                <option value="">所有状态</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-search me-1"></i> 搜索
                    </button>
                    <a asp-action="Index" class="btn btn-secondary">重置</a>
                </div>
            </form>
        </div>
    </div>
    
    <div class="card mb-4">
        <div class="card-header">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <i class="fas fa-user-tie me-1"></i>
                    客户列表
                </div>
                <div>
                    @if (User.IsInRole("Administrator") || User.IsInRole("Manager"))
                    {
                        <a asp-action="Create" class="btn btn-primary btn-sm">
                            <i class="fas fa-plus"></i> 新增客户
                        </a>
                    }
                </div>
            </div>
        </div>
        <div class="card-body">
            <table id="customersTable" class="table table-striped table-hover">
                <thead>
                    <tr>
                        <th>@Html.DisplayNameFor(model => model.CustomerNumber)</th>
                        <th>@Html.DisplayNameFor(model => model.Name)</th>
                        <th>@Html.DisplayNameFor(model => model.Type)</th>
                        <th>@Html.DisplayNameFor(model => model.ContactPerson)</th>
                        <th>@Html.DisplayNameFor(model => model.PhoneNumber)</th>
                        <th>@Html.DisplayNameFor(model => model.CreditRating)</th>
                        <th>@Html.DisplayNameFor(model => model.Status)</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                @foreach (var item in Model) {
                    <tr>
                        <td>@Html.DisplayFor(modelItem => item.CustomerNumber)</td>
                        <td>@Html.DisplayFor(modelItem => item.Name)</td>
                        <td>@Html.DisplayFor(modelItem => item.Type)</td>
                        <td>@Html.DisplayFor(modelItem => item.ContactPerson)</td>
                        <td>@Html.DisplayFor(modelItem => item.PhoneNumber)</td>
                        <td>@Html.DisplayFor(modelItem => item.CreditRating)</td>
                        <td>
                            @if (item.Status == EIMS.Models.Enums.CustomerStatus.Active)
                            {
                                <span class="badge bg-success">@Html.DisplayFor(modelItem => item.Status)</span>
                            }
                            else if (item.Status == EIMS.Models.Enums.CustomerStatus.Potential)
                            {
                                <span class="badge bg-info">@Html.DisplayFor(modelItem => item.Status)</span>
                            }
                            else if (item.Status == EIMS.Models.Enums.CustomerStatus.Inactive)
                            {
                                <span class="badge bg-warning">@Html.DisplayFor(modelItem => item.Status)</span>
                            }
                            else
                            {
                                <span class="badge bg-danger">@Html.DisplayFor(modelItem => item.Status)</span>
                            }
                        </td>
                        <td>
                            <div class="btn-group">
                                <a asp-action="Details" asp-route-id="@item.Id" class="btn btn-info btn-sm" title="详情">
                                    <i class="fas fa-info-circle"></i>
                                </a>
                                @if (User.IsInRole("Administrator") || User.IsInRole("Manager"))
                                {
                                    <a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-primary btn-sm" title="编辑">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                }
                                @if (User.IsInRole("Administrator"))
                                {
                                    <a asp-action="Delete" asp-route-id="@item.Id" class="btn btn-danger btn-sm" title="删除">
                                        <i class="fas fa-trash"></i>
                                    </a>
                                }
                            </div>
                        </td>
                    </tr>
                }
                </tbody>
            </table>
        </div>
    </div>
</div>

@section Scripts {
    <script>
        $(document).ready(function () {
            $('#customersTable').DataTable({
                language: {
                    url: '//cdn.datatables.net/plug-ins/1.13.4/i18n/zh.json'
                }
            });
        });
    </script>
} 